/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.hk.adv.service;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.util.List;
import java.util.Map;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.utils.ImgUtils;
import com.thinkgem.jeesite.modules.hk.adv.entity.HkAllAdv;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.modules.hk.adv.entity.HkMediaAdv;
import com.thinkgem.jeesite.modules.hk.adv.dao.HkMediaAdvDao;

import javax.imageio.ImageIO;

/**
 * 自媒体广告Service
 * @author 罗天文
 * @version 2018-03-15
 */
@Service
@Transactional(readOnly = true)
public class HkMediaAdvService extends CrudService<HkMediaAdvDao, HkMediaAdv> {
	@Autowired
	HkAllAdvService hkAllAdvService;
	public HkMediaAdv get(String id) {
		return super.get(id);
	}
	
	public List<HkMediaAdv> findList(HkMediaAdv hkMediaAdv) {
		return super.findList(hkMediaAdv);
	}
	
	public Page<HkMediaAdv> findPage(Page<HkMediaAdv> page, HkMediaAdv hkMediaAdv) {
		return super.findPage(page, hkMediaAdv);
	}
	
	@Transactional(readOnly = false)
	public void save(HkMediaAdv hkMediaAdv) {

		Map<String,String> map=ImgUtils.getImgWidthHeight(hkMediaAdv.getCoverImgSrc());
		hkMediaAdv.setCoverImgWidth(map.get("width"));
		hkMediaAdv.setCoverImgHeight(map.get("height"));
		hkMediaAdv.setTag(null);
		if(null!=hkMediaAdv.getTags()){
			hkMediaAdv.setTag(String.join(",", hkMediaAdv.getTags()));
		}
		super.save(hkMediaAdv);

		deleteTag(hkMediaAdv);
		insertTag(hkMediaAdv);
		deleteCategory(hkMediaAdv);
		insertCategory(hkMediaAdv);
		if(hkMediaAdv.getState().equals("1")) {
			insertAllAdv(hkMediaAdv);
		}

	}
	
	@Transactional(readOnly = false)
	public void delete(HkMediaAdv hkMediaAdv) {
		super.delete(hkMediaAdv);
	}



	/**
	 * 删除标签
	 * @param hkMediaAdv
	 * @return
	 */
	private void deleteCategory(HkMediaAdv hkMediaAdv){
		super.dao.deleteCategory(hkMediaAdv);
	}
	/**
	 * 增加标签
	 * @param hkMediaAdv
	 * @return
	 */
	private void insertCategory(HkMediaAdv hkMediaAdv){
		super.dao.insertCategory(hkMediaAdv);
	}
	//同步数据
	private void insertAllAdv(HkMediaAdv hkMediaAdv){
		String sourceId=hkMediaAdv.getId();
		HkAllAdv hkAllAdvOld =hkAllAdvService.getBySourceId(sourceId);
		if(null==hkAllAdvOld){
			HkAllAdv hkAllAdv = new HkAllAdv();
			BeanUtils.copyProperties(hkMediaAdv, hkAllAdv);
			hkAllAdv.setId(null);
			hkAllAdv.setSourceId(hkMediaAdv.getId());
			//类型  1自媒体 2企业广告3城市广告4传统文化
			hkAllAdv.setType("1");
			hkAllAdvService.save(hkAllAdv);
		}
		else{
			HkAllAdv hkAllAdv =hkAllAdvOld;
			BeanUtils.copyProperties(hkMediaAdv, hkAllAdv);
			hkAllAdv.setId(hkAllAdvOld.getId());
			hkAllAdvService.save(hkAllAdv);
		}

		hkAllAdvService.deleteTag(hkMediaAdv.getId(),hkMediaAdv.getTag());
		hkAllAdvService.insertTag(hkMediaAdv.getId(),hkMediaAdv.getTag());
		hkAllAdvService.deleteCategory(hkMediaAdv.getId(),hkMediaAdv.getCategory());
		hkAllAdvService.insertCategory(hkMediaAdv.getId(),hkMediaAdv.getCategory());
	}
	/**
	 * 删除标签
	 * @param hkMediaAdv
	 * @return
	 */
	private void deleteTag(HkMediaAdv hkMediaAdv){
		super.dao.deleteTag(hkMediaAdv);
	}
	/**
	 * 增加标签
	 * @param hkMediaAdv
	 * @return
	 */
	private void insertTag(HkMediaAdv hkMediaAdv){
		super.dao.insertTag(hkMediaAdv);
	}
}